//
//  InMemoryFile.cpp
//  FuseSQLite
//
//  Created by Admin on 6/22/14.
//  Copyright (c) 2014 Admin. All rights reserved.
//

#include "InMemoryFile.h"

InMemoryFile::InMemoryFile(std::vector<char>* fileData)
: m_fileData(fileData)
{}

size_t InMemoryFile::read(char* buffer, size_t size, size_t offset)
{
    if (offset >= m_fileData->size())
        return 0;
    
    size_t sizeToRead = std::min(m_fileData->size() - offset, size);
    std::copy(m_fileData->begin() + offset, m_fileData->begin() + offset + size, buffer);
    
    return sizeToRead;
}

size_t InMemoryFile::write(const char* buffer, size_t size, size_t offset)
{
    if (size + offset > m_fileData->size())
        m_fileData->resize(size + offset);
    
    std::copy(buffer, buffer + size, m_fileData->begin() + offset);
    
    return size;
}
